package com.quanyue.mechanics.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Date;

import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfiguration {

    private static final String DEFAULT_INCLUDE_PATTERN = "/api/.*";
    @Value("${web.token-key}")
    private String token;

    @Bean
    public Docket swaggerDocket() {
        ApiInfo apiInfo = new ApiInfo(
                "派易科技",
                "派易系统API",
                "1.0.0",
                "",
                new Contact("", "", ""),
                "",
                "",
                Collections.emptyList());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .forCodeGeneration(true)
                .genericModelSubstitutes(ResponseEntity.class)
                .globalOperationParameters(
                        Collections.singletonList(new ParameterBuilder()
                                .name(token)
                                .description("Security token")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header")
                                .required(false)
                                .build()))
                .ignoredParameterTypes(java.sql.Date.class)
                .ignoredParameterTypes(HttpServletRequest.class)
                .ignoredParameterTypes(HttpServletResponse.class)
                .directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(java.time.ZonedDateTime.class, Date.class)
                .directModelSubstitute(java.time.LocalDateTime.class, Date.class)
                .select()
                .paths(regex(DEFAULT_INCLUDE_PATTERN))
                .build();
    }
}
